package cate.game.activity.rank;

import cate.common.table.activity.rankactivity.row.RankActivityBaseRow;
import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.activity.base.core.Activity;
import cate.game.activity.base.core.ActivityHandler;
import cate.game.rank.po.Rank;
import cate.game.role.Role;

@Activity(relatedFuncIds = {
        GDFunc.Help.GOLD_COMEON_RANK,
        GDFunc.UP_POOL_STRONG_HERO_RANK,
        GDFunc.BOSS_TRAIN_RANK,
        GDFunc.Help.HERO_CALL_RANK,
        GDFunc.Help.SPICE_CALL_RANK,
        GDFunc.Help.NATURE_HERO_CALL_RANK,
        GDFunc.Help.GEMS_ON_RANK,
        GDFunc.Help.MALL_BUY_RANK,
        GDFunc.Help.ELITE_CALL_RANK,
        GDFunc.Help.M1_CONSUME_RANK,
        GDFunc.Help.GOD_EQUIP_RANK,
        GDFunc.Help.RUNE_RECAST_RANK,
        GDFunc.Help.INFORMATION_ACCEPT_RANK,
        GDFunc.BING_GUI_SHEN_SU_RANK
})
public class SingleRankActivityHandler extends ActivityHandler<SingleRankGlobalData, RoleRankActivity> {
    @Override
    public void initialize(GameBody game) throws Exception {
        super.initialize(game);
        createActivityHandler();
    }

    private void createActivityHandler(){
        RankActivityBaseRow baseRow = game.table.rankActivity.base.get(configTid());
        if(baseRow == null){
            return;
        }
        if(!game.rank.existRankHandler(baseRow.rankId)){
            BaseRankActivityHandler handler = new BaseRankActivityHandler(){
                @Override
                public int getRankId() {
                    return baseRow.rankId;
                }

                @Override
                protected String getTag() {
                    return baseRow.tag;
                }

                @Override
                public long getRoleCurrentValue(Role role) {
                    RoleRankActivity activity = role.getActivity().getEnableActivitySingle(baseRow.funcId);
                    if(activity != null){
                        return activity.rankValue;
                    }
                    return 0;
                }

                @Override
                public long getRoleCurrentExtraValue(Role role) {
                    return 0;
                }

                @Override
                public long getConditionValue() {
                    return baseRow.condition;
                }

                @Override
                protected long getExtraValue(Rank rank) {
                    return SingleRankActivityHandler.this.getExtraValue(rank.owner.uid);
                }
            };
            handler.initialize(game);
            game.rank.registerRankActivityHandler(handler);
        }
    }

    public void doRecord(Role role, int event, int param, long value, boolean add){
        RankActivityBaseRow baseRow = game.table.rankActivity.base.get(configTid());
        if(baseRow == null){
            return;
        }
        if(baseRow.event == event && baseRow.param == param){
            RoleRankActivity activity = role.getActivity().getEnableActivitySingle(baseRow.funcId);
            if(activity != null){
                long newValue = activity.record(value, add);
                activity.initSend();
                BaseRankActivityHandler handler  = ((BaseRankActivityHandler) game.rank.getRankHandler(baseRow.rankId));
                if (handler != null) {
                    handler.record(role, newValue);
                }
            }
        }
    }

    public void updateExtraValue(String roleUid, long value, boolean add) {
        getGlobalData().updateExtraRecords(roleUid, value, add);
    }

    private long getExtraValue(String roleUid) {
        return getGlobalData().getExtraRecords(roleUid);
    }

    @Override
    public String getName() {
        return "排行榜活动";
    }

    @Override
    public SingleRankGlobalData buildGlobal() {
        return new SingleRankGlobalData();
    }

    @Override
    public RoleRankActivity buildSingle() {
        return new RoleRankActivity();
    }

    @Override
    public void onActOpen() {
        createActivityHandler();
        RankActivityBaseRow baseRow = game.table.rankActivity.base.get(configTid());
        ((BaseRankActivityHandler) game.rank.getRankHandler(baseRow.rankId)).clearList();
    }

    @Override
    public void onActClose() {
        RankActivityBaseRow baseRow = game.table.rankActivity.base.get(configTid());
        // 排行榜奖励 (发完奖励后排行榜清除)
        if (baseRow != null) {
            BaseRankActivityHandler handler  = ((BaseRankActivityHandler) game.rank.getRankHandler(baseRow.rankId));
            if(handler != null){
                handler.sendReward(baseRow);
            }
        }
    }
}
